package com.ning.parking.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.ning.parking.domain.ParkingLotBaseInfo;
import com.ning.parking.domain.ParkingLotDrivewayInfo;
import com.ning.parking.domain.ParkingRecord;
import com.ning.parking.domain.VO.OnPassVO;
import com.ning.parking.domain.VO.Pagination;
import com.ning.parking.domain.dto.ParkingDTO;
import com.ning.parking.domain.dto.ParkingRecordPageDto;
import com.ning.parking.service.OnPassService;
import com.ning.parking.service.ParkingLotBaseInfoService;
import com.ning.parking.service.ParkingLotDrivewayInfoService;
import com.ning.parking.service.ParkingRecordService;
import com.scsonic.common.enums.StatusEnum;
import com.scsonic.common.enums.parklot.DrivewayTypeEnum;
import com.scsonic.common.enums.trade.TradePlatformEnum;
import com.scsonic.framework.common.response.R;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author ChenN
 * @Date 2025/3/20 9:24
 * @Version 0.0.1
 **/
@Slf4j
@RestController
@RequestMapping("box")
@AllArgsConstructor
public class SentryBoxController {

    final ParkingLotBaseInfoService parkingLotBaseInfoService;
    final ParkingLotDrivewayInfoService parkingLotDrivewayInfoService;
    final ParkingRecordService parkingRecordService;
    final OnPassService onPassService;

    /**
     * 进出场事件
     * @param parkingDTO
     * @return
     */
    @GetMapping("car-event")
    public R<OnPassVO> carEvent(ParkingDTO parkingDTO) {
        log.debug("car-event:{}", JSONUtil.parseObj(parkingDTO));
        return R.ok(onPassService.onPass(parkingDTO));
    }

    /**
     * 补入无入场记录
     * @param parkingDTO
     * @return
     */
    @GetMapping("fill-in-record-info")
    public R<OnPassVO> fillInRecordInfo(ParkingDTO parkingDTO) {
        log.debug("car-event:{}", JSONUtil.parseObj(parkingDTO));
        return R.ok(onPassService.fillInRecordInfo(parkingDTO));
    }

    /**
     * 获取车位数
     * @param parkingLotId
     * @return
     */
    @GetMapping("get-places")
    public R<ParkingLotBaseInfo>getPlaces(Long parkingLotId) {
        ParkingLotBaseInfo parkingLotBaseInfoServiceById = parkingLotBaseInfoService.getById(parkingLotId);
        Integer emptyPlaces = parkingLotBaseInfoServiceById.getEmptyPlaces();
        emptyPlaces = emptyPlaces > 0 ? emptyPlaces : 0;
        parkingLotBaseInfoServiceById.setEmptyPlaces(emptyPlaces);
        return R.ok(parkingLotBaseInfoServiceById);
    }

    /**
     * 获取通道最后一次通车信息
     * @param drivewayId
     * @return
     */
    @GetMapping("get-last-car")
    public R<ParkingRecord> getLastCar(Long drivewayId) {

        ParkingRecord one = parkingRecordService.getLastCar(drivewayId);
        return R.ok(one);
    }

    /**
     * 获取停车记录信息
     * @param recordId
     * @return
     */
    @GetMapping("get-record-by-id")
    public R<ParkingRecord> getRecordById(Long recordId) {
        return R.ok(parkingRecordService.getById(recordId));
    }

    /**
     * 获取车位信息
     * @param parkingLotId
     * @return
     */
    @GetMapping("get-places-number")
    public R<ParkingLotBaseInfo> getPlacesNumber(Long parkingLotId) {
        ParkingLotBaseInfo parkingLotBaseInfo = parkingLotBaseInfoService.getById(parkingLotId);
        return R.ok(parkingLotBaseInfo);
    }


    /**
     * 支付
     * @param parkingRecord
     * @return
     */
    @PostMapping("payment")
    public R<ParkingRecord> payment(@RequestBody ParkingRecord parkingRecord) {
        return R.ok(parkingRecordService.payment(parkingRecord));
    }

    /**
     * 获取在场车记录
     * @param pageBase
     * @return
     */
    @GetMapping("get-in-parking-page")
    public R<Pagination<ParkingRecord>> getInParkingPage(ParkingRecordPageDto pageBase) {
        log.debug("get-in-parking-page:{}", JSONUtil.parseObj(pageBase));
        pageBase.setInPark(StatusEnum.YES);
        return R.ok(parkingRecordService.selectPage(pageBase));
    }

    /**
     * 进出场事件
     * @param parkingDTO
     * @return
     */
    @GetMapping("personnel-car-exit")
    public R<OnPassVO> personnelCarExit(ParkingDTO parkingDTO) {
        log.debug("personnelCarExit:{}", JSONUtil.parseObj(parkingDTO));
        return R.ok(onPassService.personnelCarExit(parkingDTO));
    }

}
